package battlebarnapplet;

import battlebarnapplet.entities.ClientProjectileEntity;
import battlebarnserver.util.Constants;
import battlebarnserver.util.Conversions;

import java.util.Iterator;
import java.util.Map;

/**
 * 
 * @author johncreecy
 */
public class Animations {

	public void moveProjectiles() {
		int pixelX;
		int pixelY;
		ClientProjectileEntity clientProjectileEntity;
		ClientProjectileEntity previousClientProjectileEntity;

		// start new projectiles
		Iterator it = Board.projectiles.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry mapEntry = (Map.Entry) it.next();
			clientProjectileEntity = (ClientProjectileEntity) mapEntry.getValue();
			Board.projectileLocs.add(clientProjectileEntity);
			Board.projectiles.remove(clientProjectileEntity.getProjectileId());
		}
	
		// move existing projectiles
		for (int i = 0; i < Board.projectileLocs.size(); i++) {
			previousClientProjectileEntity = Board.projectileLocs.get(i);
			Board.projectileLocs.remove(i);
			if (Board.removeProjectiles.contains(previousClientProjectileEntity.getProjectileId())) {
				Board.removeProjectiles.remove(previousClientProjectileEntity.getProjectileId());
			} else {
				pixelX = previousClientProjectileEntity.getPixelX() + (previousClientProjectileEntity.getVelocityX() * Constants.CLIENT_PROJECTILE_TRAVEL_PER_FRAME);
				pixelY = previousClientProjectileEntity.getPixelY() + (previousClientProjectileEntity.getVelocityY() * Constants.CLIENT_PROJECTILE_TRAVEL_PER_FRAME);
				clientProjectileEntity = new ClientProjectileEntity(previousClientProjectileEntity.getProjectileId(), previousClientProjectileEntity.getVelocityX(), previousClientProjectileEntity.getVelocityY(), pixelX, pixelY);
				if (Conversions.testPixelBoundsX(pixelX) && Conversions.testPixelBoundsY(pixelY)) {
					Board.projectileLocs.add(clientProjectileEntity);
				}
			}
		}
	}
	

}
